/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package easyanimation.frame;

import easyanimation.panel.CCenterPanel;
import easyanimation.panel.CLeftPanel;
import easyanimation.panel.CRightPanel;
import easyanimation.panel.CStatusPanel;
import easyanimation.panel.CToolbarPanel;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Enumeration;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.KeyStroke;
import javax.swing.LookAndFeel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.MenuEvent;
import javax.swing.event.MenuListener;
import javax.swing.plaf.FontUIResource;
import javax.swing.plaf.metal.MetalLookAndFeel;

/**
 *
 * @author Ngoc Tan
 */

public class MainWindow extends JFrame implements KeyListener
{
    //window instance
    private static MainWindow m_window;
    //panel
    private CToolbarPanel m_objToolbar;
    private CLeftPanel m_objLeftPanel;
    private CCenterPanel m_objCenterPanel;
    private JScrollPane m_objCenterJScrollPane;
    private CRightPanel m_objRightPanel;
    private CStatusPanel m_objStatusPanel;
    //For center panel
    private Dimension m_objCenterSize;
    //menu
    private JMenuBar m_objMenuBar;
    private JMenu m_objFileMenu;
    private JMenu m_objEditMenu;
    private JMenu m_objHelpMenu;
    private JMenu m_objAboutMenu;
    private JMenuItem m_objFileNew;
    private JMenuItem m_objFileOpen;
    private JMenuItem m_objFileSave;
    private JMenuItem m_objFileSaveAs;

    private MainWindow()
    {
        //modified default font
        setFont();
        //init menu        
        initMenuBar();
        //init panel
        m_objToolbar = new CToolbarPanel();
        m_objLeftPanel = new CLeftPanel();
        m_objRightPanel = new CRightPanel();
        m_objCenterPanel = new CCenterPanel();
        m_objStatusPanel = new CStatusPanel(0, 0);
        //
        // set attributes
        setTitle(Language.NAME[CConfig.LANGUAGE] + CConfig.VERSION);
        setSize(Constant.WINDOW_WIDTH, Constant.WINDOW_HEIGHT);
        setLayout(new BorderLayout());
        //add panel
        add(m_objToolbar, BorderLayout.NORTH);
        add(m_objLeftPanel, BorderLayout.WEST);
        
        JPanel centerPanel = new JPanel(new BorderLayout());
        centerPanel.add(m_objStatusPanel, BorderLayout.SOUTH);
        
        m_objCenterJScrollPane = new JScrollPane(m_objCenterPanel);
        m_objCenterSize = m_objCenterJScrollPane.getSize();
        m_objCenterJScrollPane.setAutoscrolls(true);
        m_objCenterJScrollPane.setViewportBorder(BorderFactory.createLineBorder(Color.BLACK));
        centerPanel.add(m_objCenterJScrollPane, BorderLayout.CENTER);          
        add(centerPanel, BorderLayout.CENTER);
        
        add(m_objRightPanel, BorderLayout.EAST);

        //add key escape for exit window
        getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "exit");
        getRootPane().getActionMap().put("exit", new AbstractAction()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                closeWindow();
            }
        });
        //add listener for the window
        addComponentListener(new ComponentListener()
        {
            @Override
            public void componentResized(ComponentEvent e)
            {
                m_objCenterSize = m_objCenterJScrollPane.getSize();
                MainWindow.getInstance().getRightPanel().setIndexStart(0);
                MainWindow.getInstance().getCenterPanel().reupdateUI();
                MainWindow.getInstance().getRightPanel().reupdateUI();
            }

            @Override
            public void componentMoved(ComponentEvent e)
            {
            }

            @Override
            public void componentShown(ComponentEvent e)
            {
            }

            @Override
            public void componentHidden(ComponentEvent e)
            {
            }
        });
        //
        m_objLeftPanel.addChangeListener(new ChangeListener()
        {
            @Override
            public void stateChanged(ChangeEvent e)
            {
                m_objCenterJScrollPane.updateUI();
            }
        });
        //UI look and feel
        setLookAndFeel(new MetalLookAndFeel());
        //add key listener for components
        //m_leftPanel.getTabModule().getTable().getEditorComponent().addKeyListener(this);
        //m_centerPanel.requestFocusInWindow();

    }

    public synchronized static MainWindow getInstance()
    {
        if (m_window == null)
        {
            m_window = new MainWindow();
        }
        return m_window;
    }

    private void setLookAndFeel(LookAndFeel lookAndFeel)
    {
        try
        {
            //MetalLookAndFeel.setCurrentTheme(CMetalTheme.DEFAULT);            
            UIManager.setLookAndFeel(lookAndFeel);
        } catch (UnsupportedLookAndFeelException ex)
        {
            Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    private void setFont()
    {
        Enumeration objEnumKey =  UIManager.getDefaults().keys();
        while(objEnumKey.hasMoreElements())
        {
            Object objKey = objEnumKey.nextElement();
            if(UIManager.get(objKey) instanceof FontUIResource)
                UIManager.put(objKey, new FontUIResource(Constant.OBJ_FONT_BOLD));
        }
    }

    private void initMenuBar()
    {
        //init menubar
        m_objMenuBar = new JMenuBar();
        //add menu file
        m_objFileMenu = new JMenu(Language.MENU_FILE[CConfig.LANGUAGE]);
        m_objFileMenu.setMnemonic(Language.MENU_FILE[CConfig.LANGUAGE].charAt(0));
        m_objFileNew = new JMenuItem(Language.MENU_FILE_NEW[CConfig.LANGUAGE], null);
        m_objFileNew.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, KeyEvent.CTRL_MASK));
        m_objFileMenu.add(m_objFileNew);
        m_objFileOpen = new JMenuItem(Language.MENU_FILE_OPEN[CConfig.LANGUAGE], null);
        m_objFileMenu.add(m_objFileOpen);
        m_objFileSave = new JMenuItem(Language.MENU_FILE_SAVE[CConfig.LANGUAGE], null);
        m_objFileMenu.add(m_objFileSave);
        m_objFileSaveAs = new JMenuItem(Language.MENU_FILE_SAVE_AS[CConfig.LANGUAGE], null);
        m_objFileMenu.add(m_objFileSaveAs);

        m_objMenuBar.add(m_objFileMenu);
        //add menu edit
        m_objEditMenu = new JMenu(Language.MENU_EDIT[CConfig.LANGUAGE]);
        m_objEditMenu.setMnemonic(Language.MENU_EDIT[CConfig.LANGUAGE].charAt(0));
        m_objMenuBar.add(m_objEditMenu);
        //add menu help
        m_objHelpMenu = new JMenu(Language.MENU_HELP[CConfig.LANGUAGE]);
        m_objHelpMenu.setMnemonic(Language.MENU_HELP[CConfig.LANGUAGE].charAt(0));
        m_objMenuBar.add(m_objHelpMenu);
        //add menu about
        m_objAboutMenu = new JMenu(Language.MENU_ABOUT[CConfig.LANGUAGE]);
        m_objAboutMenu.setMnemonic(Language.MENU_ABOUT[CConfig.LANGUAGE].charAt(0));
        m_objAboutMenu.addMenuListener(new MenuListener()
        {
            @Override
            public void menuSelected(MenuEvent e)
            {
                JOptionPane.showMessageDialog(m_window, "afjkasfjkasjf");
            }

            @Override
            public void menuDeselected(MenuEvent e)
            {
            }

            @Override
            public void menuCanceled(MenuEvent e)
            {
            }
        });
        m_objMenuBar.add(m_objAboutMenu);

        //add to frame
        this.setJMenuBar(m_objMenuBar);
    }

    private void closeWindow()
    {
        //Save data
        //close
        System.exit(0);
    }

    /**
     * @return the m_toolbar
     */
    public CToolbarPanel getToolbar()
    {
        return m_objToolbar;
    }

    /**
     * @return the m_leftPanel
     */
    public CLeftPanel getLeftPanel()
    {
        return m_objLeftPanel;
    }

    /**
     * @return the m_centerPanel
     */
    public CCenterPanel getCenterPanel()
    {
        return m_objCenterPanel;
    }

    /**
     * @param m_statusPanel the m_statusPanel to set
     */
    public CStatusPanel getStatusPanel()
    {
        return m_objStatusPanel;
    }

    /**
     * @return the m_centerJScrollPane
     */
    public JScrollPane getCenterJScrollPane()
    {
        return m_objCenterJScrollPane;
    }

    /**
     * @return the m_centerSize
     */
    public Dimension getCenterSize()
    {
        return m_objCenterSize;
    }

    @Override
    public void keyTyped(KeyEvent e)
    {
    }

    @Override
    public void keyPressed(KeyEvent e)
    {
    }

    @Override
    public void keyReleased(KeyEvent e)
    {
    }

    /**
     * @return the m_objRightPanel
     */
    public CRightPanel getRightPanel()
    {
        return m_objRightPanel;
    }
}

